package initialize

import (
	_ "gin-demo/docs"
	"gin-demo/global"
	"gin-demo/middleware"
	"gin-demo/router"
	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	"net/http"
)

func InitRouter() *gin.Engine {
	if global.GN_CONFIG.System.Mode == gin.TestMode {
		gin.SetMode(gin.TestMode)
	} else if global.GN_CONFIG.System.Mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
	} else {
		gin.SetMode(gin.DebugMode)
	}

	r := gin.Default()
	fs := global.GN_CONFIG.Local.Path
	r.StaticFS(fs, http.Dir("./"+fs))

	// 跨域
	r.Use(middleware.Cors()) // 如需跨域可以打开
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	global.GN_LOG.Info("register swagger handler")

	rGroup := r.Group("/v1")
	{
		authGroup := rGroup.Group("/auth")
		{
			router.InitAuthRouter(authGroup)
		}

		userGroup := rGroup.Group("/user")
		userGroup.Use(middleware.AuthMiddleware())
		{
			router.InitUserRouter(userGroup)
		}

		attachGroup := rGroup.Group("/attach")
		attachGroup.Use(middleware.AuthMiddleware())
		{
			router.InitAttachRouter(attachGroup)
		}
	}

	return r
}